﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MvcShop.Models.ViewDataModels;
using System.Data.Objects;

namespace MvcShop.Models.Repositories
{
    public class ProductRepository: BaseRepositoryBase<Product>, IProductRepository<Product>
    {
        public ProductRepository()
        {
            this.EntitiesSetName = "Products";
        }

        public void Del(IEnumerable<int> values)
        {
            var query = this.EntitySet.Where(ContainsCriteria.ContainsExpression<Product, int>(c => c.ProductId, values)).ToList();
            foreach (var value in query)
            {
                this.dataContext.DeleteObject(value);
            }
            this.SaveChanges();
        }

        public int GetProductId(string productName) 
        {
            var query = from p in this.EntitySet
                        where p.ProductName == productName
                        select p.ProductId;

            return query.FirstOrDefault();
        }

        public IEnumerable<ProductList> GetCategoryHotSell(int categoryId)
        {
            var query = (from p in this.EntitySet
                         where p.CategoryId == categoryId
                         orderby p.SellAmount descending
                         select new ProductList
                         {
                             CategoryId = categoryId,
                             ProductId = p.ProductId,
                             ProductName = p.ProductName
                         }).Take(10).ToList();

            return query;
        } 
    }

    public class CategoryRepository : BaseRepositoryBase<Category>, ICategoryRepository<Category>
    {
        public CategoryRepository()
        {
            this.EntitiesSetName = "Categories";
        }

        public IEnumerable<CategoryNavigator> GetCNavigator()
        {
            var query = (from ca in this.EntitySet
                        where ca.ParentId ==1
                        orderby ca.Position
                         select new CategoryNavigator { CategoryId = ca.CategoryId, CategoryName = ca.CategoryName }).ToList();

            foreach(var item in query)
            {
                item.CNode = (from cat in this.EntitySet
                              where cat.ParentId == item.CategoryId
                              orderby cat.Position
                              select new CNode { CategoryId = cat.CategoryId, CategoryName = cat.CategoryName }).ToList();
            }

            return query;
        }

        public int GetMaxPosition()
        {
            var query = this.EntitySet.Max(c => c.Position).GetValueOrDefault();
            return query;
        }
    }

    public class Product_propertys_contentRepository : BaseRepositoryBase<Product_propertys_content>, IProduct_propertys_contentRepository<Product_propertys_content>
    {
        public Product_propertys_contentRepository()
        {
            this.EntitiesSetName = "Product_propertys_contents";
        }

        public void Del(IEnumerable<int> values)
        {
            var query = this.EntitySet.Where(ContainsCriteria.ContainsExpression<Product_propertys_content, int>(c => c.ProductId, values)).ToList();
            foreach (var value in query)
            {
                this.dataContext.DeleteObject(value);
            }
            this.SaveChanges();
        }
    }

    public class Product_propertyRepository : BaseRepositoryBase<Product_property>, IProduct_propertyRepository<Product_property>
    {
        public Product_propertyRepository()
        {
            this.EntitiesSetName = "Product_properties";
        }
    }

    public class Product_ImageRepository : BaseRepositoryBase<Product_Image>, IProduct_ImageRepository<Product_Image>
    {
        public Product_ImageRepository()
        {
            this.EntitiesSetName = "Product_Images";
        }

        public IList<Product_Image> Find(IEnumerable<int> values)
        {
            var query = this.EntitySet.Where(ContainsCriteria.ContainsExpression<Product_Image, int>(c => c.ProductId, values)).ToList();
            return query;
        }

        public IList<FrontProductImage> GetListThumbnailImage(int productId)
        {
            var query = (from ima in this.EntitySet
                        where ima.ProductId == productId
                        select new FrontProductImage
                        {
                            Product_ImageId = ima.Product_ImageId,
                            ProductId = ima.ProductId,
                            ImageAddress = ima.ImageAddress
                            //ThumbnailImageAddress = ima.ImageAddress.Split('.')[0] + "-thumnail.png"
                        }).ToList();

            return query;
        }
    }

    public class RemarkRepository : BaseRepositoryBase<Remark>, IRemarkRepository<Remark>
    {
        public RemarkRepository()
        {
            this.EntitiesSetName = "Remarks";
        }

        public IList<Remark> DefaultListRemarks(int productId)
        {
            var query = this.EntitySet.Where(c => c.ProductId == productId && c.RemarkType == 1).Take(20).ToList();
            return query;
        }
    }
}
